## Figure k1: Covid-related tweet

rm(list = ls())

setwd('/path/to/replication/')

library(tidyverse)
library(data.table)
library(ggplot2)

tweets_weekly = fread('./data/covid_share_weekly_data.csv')

## Subfigure A

plot1.table = tweets_weekly %>%
  group_by(week, date, losing_income_d) %>%
  summarise_at(.vars = 'share_of_covid', .funs = c(mean, sd), na.rm = TRUE) %>%
  rename('mean' = 'fn1',
         'std' = 'fn2') %>%
  inner_join(tweets_weekly %>%
               group_by(week, date, losing_income_d) %>%
               summarise(count = n()),
             by = c('week','losing_income_d', 'date')) %>%
  mutate(ub = mean + (std / sqrt(count))*1.96,
         lb = mean - (std / sqrt(count))*1.96)

plot1 = ggplot(plot1.table, aes(x = date, y = mean, group = losing_income_d)) +
  geom_line(aes(color = losing_income_d), size = 1) +
  scale_x_date(breaks = "7 day", date_labels = '%Y-%m-%d',
               limits = c(min(plot1.table$date), max = max(plot1.table$date)),
               expand=c(0,0)) +
  xlab("") +
  ylab("Covid-related tweets' share") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"
        ),axis.title.y = element_text(size = rel(1.2)), axis.title.x = element_text(size = rel(1.1)),
        legend.position = "bottom") +
  guides(color=guide_legend(title=""))

## Subfigure B

plot1.table.mort = tweets_weekly %>%
  filter(!is.na(excess_deaths_jan_d)) %>%
  group_by(week, date, excess_deaths_jan_d) %>%
  summarise_at(.vars = 'share_of_covid', .funs = c(mean, sd), na.rm = TRUE) %>%
  rename('mean' = 'fn1',
         'std' = 'fn2') %>%
  inner_join(tweets_weekly %>%
               group_by(week, date, excess_deaths_jan_d) %>%
               summarise(count = n()),
             by = c('week','excess_deaths_jan_d', 'date')) %>%
  mutate(ub = mean + (std / sqrt(count))*1.96,
         lb = mean - (std / sqrt(count))*1.96)

plot1.mort = ggplot(plot1.table.mort, aes(x = date, y = mean, group = excess_deaths_jan_d)) +
  geom_line(aes(color = excess_deaths_jan_d), size = 1) +
  scale_x_date(breaks = "7 day", date_labels = '%Y-%m-%d',
               limits = c(min(plot1.table$date), max = max(plot1.table$date)),
               expand=c(0,0)) +
  xlab("") +
  ylab("Covid-related tweets' share") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"
        ),axis.title.y = element_text(size = rel(1.2)), axis.title.x = element_text(size = rel(1.1)),
        legend.position = "bottom") +
  guides(color=guide_legend(title=""))

ggsave(plot = plot1, filename = './figureK1_a.jpeg', width=6, height=4.5)
ggsave(plot = plot1.mort, filename = './figureK1_b.jpeg', width=6, height=4.5)
